package com.klm.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * 签名工具类 - 客户端使用
 */
public class SignUtil {
    
    /**
     * 生成签名
     * @param appSecret 应用密钥
     * @param timestamp 时间戳
     * @return 签名字符串
     */
    public static String generateSignature(String appSecret, long timestamp) {
        try {
            String data = timestamp + "";
            Mac mac = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKeySpec = new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
            mac.init(secretKeySpec);
            byte[] hash = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(hash);
        } catch (Exception e) {
            throw new RuntimeException("Failed to generate signature", e);
        }
    }
}
